package com.itqf.ipmsys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itqf.ipmsys.dto.TrafficDto;
import com.itqf.ipmsys.dto.TrafficListDTO;
import com.itqf.ipmsys.entity.Traffic;
import com.itqf.ipmsys.mapper.TrafficMapper;
import com.itqf.ipmsys.service.TrafficService;
import com.itqf.ipmsys.vo.TrafficVO;
import com.itqf.ipmsys.vo.UploadVO;
import com.itqf.ipmsys.vo.tarUnVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 交通状况表 服务实现类
 * </p>
 *
 * @author corn
 * @since 2021-09-22
 */
@Service
public class TrafficServiceImpl extends ServiceImpl<TrafficMapper, Traffic> implements TrafficService {

    @Resource
    private TrafficMapper trafficMapper;

    /**
     * 查询路况上传列表
     *
     * @return
     */
    @Override
    public List<TrafficVO> findTrafficList(TrafficListDTO trafficListDTO) {
        int start = (trafficListDTO.getPage() - 1) * trafficListDTO.getPageSize();
        if (!StringUtils.isEmpty(trafficListDTO.getUnit())) {
            trafficListDTO.setUnit("%" + trafficListDTO.getUnit() + "%");
        }
        List<TrafficVO> list = trafficMapper.selectTrafficList(trafficListDTO, start);
        return list;
    }

    /**
     * 查询总数
     *
     * @param trafficListDTO
     * @return
     */
    @Override
    public int findTrafficCount(TrafficListDTO trafficListDTO) {
        if (!StringUtils.isEmpty(trafficListDTO.getUnit())) {
            trafficListDTO.setUnit("%" + trafficListDTO.getUnit() + "%");
        }
        Integer count = trafficMapper.selectTrafficCount(trafficListDTO);
        return count;
    }

    @Override
    public TrafficVO findTrafficDetail(Integer id) {
        TrafficVO trafficVo = trafficMapper.selectTrafficDetail(id);
        return trafficVo;
    }

    @Override
    public int removeTrafficById(Integer id) {
        int rows = trafficMapper.deleteById(id);
        return rows;
    }

    @Override
    public List<UploadVO> select(TrafficDto trafficDto) {
        List<UploadVO> list = new ArrayList<>();
        List<String> hasNames  = trafficMapper.findAll(trafficDto);
        List<String> allNames  = trafficMapper.selectAllName();
        for (String allName : allNames) {
            boolean flag = true;
            for (String hasName : hasNames) {
                if(allName.equals(hasName)){
                    list.add(new UploadVO(allName,1));
                    flag = false;
                    break;
                }
            }
            if(flag){
                list.add(new UploadVO(allName, 0));
            }
        }
        return list;
    }
}
